package org.springblade.mes.purchase.order.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.purchase.order.entity.OrderDetail;
import org.springblade.mes.purchase.order.mapper.OrderDetailMapper;
import org.springblade.mes.purchase.order.model.OrderDetailBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/** 
 * 服务
 * @Version 1.0
 */
@Slf4j
@Service
@DS("purchase")
public class OrderDetailService extends BaseServiceImpl<OrderDetailMapper, OrderDetail> {
	
	@Transactional(rollbackFor = Exception.class)
	public boolean addData(OrderDetailBean param) throws Exception{
		//保存
		OrderDetail entity = param;
		return this.save(entity);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public boolean updateData(OrderDetailBean param) throws Exception{
		//更新
		OrderDetail newEntity = param;
		return this.updateById(newEntity);
	}

	
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteData(OrderDetailBean param) throws Exception{
		if(param.getIdList() != null && param.getIdList().size() >0){
			UpdateWrapper<OrderDetail> del = new UpdateWrapper<>();
			del.lambda().in(OrderDetail::getId, param.getIdList());
			return this.remove(del);
		}
		if (param.getId() != null) {
            return this.removeById(param.getId());
        }
		return true;
	}

	
	public OrderDetailBean findById(Long id) {
		OrderDetailBean cond = new OrderDetailBean();
		cond.setId(id);
		return findBySpec(cond);
    }

	
	public OrderDetailBean findBySpec(OrderDetailBean param) {
		List<OrderDetailBean> list = this.baseMapper.queryList(param);
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	
	public List<OrderDetailBean> findListBySpec(OrderDetailBean param) {
		return this.baseMapper.queryList(param);
	}

	
	public IPage<OrderDetailBean> findPageBySpec(IPage<OrderDetailBean> page, OrderDetailBean param) {
		List<OrderDetailBean> dataList = baseMapper.queryPageList(page, param);
		return page.setRecords(dataList);
	}

	/**
	 * 字典赋值
	 */
	private List<OrderDetailBean> fillDict(List<OrderDetailBean> dataList) {
		if (dataList == null || dataList.size() == 0) {
			return Collections.emptyList();
		}
		for (OrderDetailBean vo : dataList) {
			//TODO
		}
		return dataList;
	}
}

